﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.SystemFramework;
using OrderSystem.Templet;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormUpdateProductType : FormTemplet
    {
        private int _nProductTypeID = -1;

        public FormUpdateProductType()
        {
            this.InitializeComponent();
        }

        public FormUpdateProductType(int nProductTypeID) : this()
        {
            this._nProductTypeID = nProductTypeID;
            DataTable dataTable = null;
            ProductTypes productTypes = new ProductTypes();
            try
            {
                dataTable = productTypes.LoadProductType(this._nProductTypeID);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            productTypes.Dispose();
            if (dataTable != null)
            {
                if (dataTable.Rows.Count > 0)
                {
                    this.txtProductName.Text = dataTable.Rows[0]["类型名称"].ToString();
                    this.txtThick.Text = dataTable.Rows[0]["厚度规格"].ToString();
                    this.txtUnit.Text = dataTable.Rows[0]["单位"].ToString();
                    this.txtDefaultPrice.Text = dataTable.Rows[0]["默认价格"].ToString();
                    this.txtLabelMultiple.Text = dataTable.Rows[0]["标签倍数"].ToString();
                    this.txtRemark.Text = dataTable.Rows[0]["备注"].ToString();
                    this.txtLimitedQuantity.Text = dataTable.Rows[0]["流程卡数量上限"].ToString();
                    this.txtLimitedArea.Text = dataTable.Rows[0]["流程卡面积上限"].ToString();
                    this.btnOk.Enabled = true;
                }
                else
                {
                    MessageBox.Show("数据库里没有查到此 产品类型 的数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                dataTable.Dispose();
            }
            if (LoginInfo.Role.RoleID != 0)
            {
                this.txtProductName.ReadOnly = true;
                this.txtProductName.BackColor = SystemColors.InactiveCaptionText;
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.txtProductName.Text = CommonMethod.Fun1(this.txtProductName.Text);
            this.txtThick.Text = CommonMethod.Fun1(this.txtThick.Text);
            this.txtUnit.Text = CommonMethod.Fun1(this.txtUnit.Text);
            this.txtDefaultPrice.Text = CommonMethod.Fun1(this.txtDefaultPrice.Text);
            this.txtLabelMultiple.Text = CommonMethod.Fun1(this.txtLabelMultiple.Text);
            this.txtRemark.Text = CommonMethod.Fun1(this.txtRemark.Text);
            this.txtLimitedQuantity.Text = CommonMethod.Fun1(this.txtLimitedQuantity.Text);
            this.txtLimitedArea.Text = CommonMethod.Fun1(this.txtLimitedArea.Text);
            if (this.txtProductName.Text.Trim() == "" || this.txtProductName.Text.Trim().Length > 128)
            {
                MessageBox.Show("产品类型的 长度 应该在 1-128 个字符之间 ！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtProductName.Focus();
            }
            else if (this.txtThick.Text.Trim() == "" || this.txtThick.Text.Trim().Length > 16)
            {
                MessageBox.Show("产品类型的 厚度 应在 1-16 个字符之间", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtThick.Focus();
            }
            else if (this.txtUnit.Text.Trim() == "" || this.txtUnit.Text.Trim().Length > 8)
            {
                MessageBox.Show("产品类型的 单位 应在 1-8 个字符之间", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtUnit.Focus();
            }
            else if (!Regex.IsMatch(this.txtDefaultPrice.Text.Trim(), "^(([0-9]{1,})|([0-9]{1,}\\.[0-9]{1,2}))$") || Convert.ToDecimal(this.txtDefaultPrice.Text.Trim()) < 0m)
            {
                MessageBox.Show("价格 格式不正确，此处应该为不为负的数字，如有小数，最大为2位小数！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtDefaultPrice.Focus();
            }
            else if (!Regex.IsMatch(this.txtLabelMultiple.Text, "^[0-9]{1,}$") || Convert.ToInt32(this.txtLabelMultiple.Text.Trim()) <= 0)
            {
                MessageBox.Show("标签倍数 格式不正确，此处应该为大于零的正整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtLabelMultiple.Focus();
            }
            else if (this.txtRemark.Text.Trim().Length > 64)
            {
                MessageBox.Show("产品类型的 备注 应在 64 个字符以内", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtRemark.Focus();
            }
            else if (!Regex.IsMatch(this.txtLimitedQuantity.Text, "^[0-9]{1,}$") || Convert.ToInt32(this.txtLimitedQuantity.Text.Trim()) < 1)
            {
                MessageBox.Show("流程卡数量上限 格式不正确，此处应该为大于 1 的整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtLimitedQuantity.Focus();
            }
            else if (!Regex.IsMatch(this.txtLimitedArea.Text.Trim(), "^(([0-9]{1,8})|([0-9]{1,8}\\.[0-9]{1,2}))$") || Convert.ToDecimal(this.txtLimitedArea.Text.Trim()) <= 0m)
            {
                MessageBox.Show("流程卡面积上限 格式不正确，此处应该为大于 0 的正整数，如有小数，最大为 2 位小数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.txtLimitedArea.Focus();
            }
            else if (MessageBox.Show("更改类型后，之前使用此类型的数据将随之改变，请慎重操作！\r\n\r\n是否继续更改？", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) != DialogResult.No)
            {
                ProductTypes productTypes = new ProductTypes();
                bool flag = false;
                try
                {
                    flag = productTypes.UpdateProductType(this._nProductTypeID, this.txtProductName.Text.Trim(), this.txtThick.Text.Trim(), this.txtUnit.Text.Trim(), Convert.ToDecimal(this.txtDefaultPrice.Text.Trim()), Convert.ToInt32(this.txtLabelMultiple.Text.Trim()), this.txtRemark.Text.Trim(), Convert.ToInt32(this.txtLimitedQuantity.Text.Trim()), Convert.ToDecimal(this.txtLimitedArea.Text.Trim()));
                }
                catch (SqlException ex)
                {
                    if (ex.Number == 2627)
                    {
                        MessageBox.Show("这个产品类型已经存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtProductName.Focus();
                    }
                    else
                    {
                        MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                productTypes.Dispose();
                if (flag)
                {
                    MessageBox.Show("产品类型更改成功！", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    base.Close();
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }
}
